package org.zyx.sms.listener;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.zyx.sms.config.SmsProperties;
import org.zyx.sms.utils.SmsUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author: 98050
 * @Time: 2018-10-22 19:21
 * @Feature:短信服务监听器
 */
@Component
public class SmsListener {

    @Resource
    private SmsUtils smsUtils;

    @Resource
    private SmsProperties smsProperties;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "yiyou.sms.queue", durable = "true"),
            exchange = @Exchange(value = "yiyou.sms.exchange", ignoreDeclarationExceptions = "true"),
            key = {"sms.verify.code"}
    ))
    public void listenSms(Map<String, String> msg) {
        if (msg == null || msg.size() <= 0) {
            //不做处理
            return;
        }
        String phone = msg.get("phone");
        String code = msg.get("code");

        if (StringUtils.isNotBlank(phone) && StringUtils.isNotBlank(code)) {
            //发送消息
            try {
                SendSmsResponse response = this.smsUtils.sendSms(phone, code, smsProperties.getSignName(), smsProperties.getVerifyCodeTemplate());
            } catch (ClientException e) {
                return;
            }
        } else {
            //不做处理
            return;
        }
    }
}
